// CppUnit-Tutorial
// file: CTestFraction.cc
#include "CTestFraction.h"

CPPUNIT_TEST_SUITE_REGISTRATION (CTestFraction);

void CTestFraction::setUp (void)
    {
    // set up test environment (initializing objects)
    a = new CFraction (1, 2);
    b = new CFraction (2, 3);
    c = new CFraction (2, 6);
    d = new CFraction (-5, 2);
    e = new CFraction (5, -2);
    f = new CFraction (-5, -2);
    g = new CFraction (5, 2);
    h = new CFraction ();
    }

void CTestFraction::tearDown (void)
    {
    // finally delete objects
    delete a; delete b; delete c; delete d;
    delete e; delete f; delete g; delete h;
    }

void CTestFraction::addTest (void)
    {
    // check subtraction results
    CPPUNIT_ASSERT_EQUAL (*a + *b, CFraction (7, 6));
    CPPUNIT_ASSERT_EQUAL (*b + *c, CFraction (1));
    CPPUNIT_ASSERT_EQUAL (*d + *e, CFraction (-5));
    CPPUNIT_ASSERT_EQUAL (*e + *f, CFraction (0));
    CPPUNIT_ASSERT_EQUAL (*h + *c, CFraction (2, 6));
    CPPUNIT_ASSERT_EQUAL (*a + *b + *c + *d + *e + *f + *g + *h, CFraction (3, 2));
    }

void CTestFraction::subTest (void)
    {
    // check addition results
    CPPUNIT_ASSERT_EQUAL (*a - *b, CFraction (-1, 6));
    CPPUNIT_ASSERT_EQUAL (*b - *c, CFraction (1, 3));
    CPPUNIT_ASSERT_EQUAL (*b - *c, CFraction (2, 6));
    CPPUNIT_ASSERT_EQUAL (*d - *e, CFraction (0));
    CPPUNIT_ASSERT_EQUAL (*d - *e - *f - *g - *h, CFraction (-5));
    }

void CTestFraction::exceptionTest (void)
    {
    // an exception has to be thrown here
    CPPUNIT_ASSERT_THROW (CFraction (1, 0), DivisionByZeroException);
    }

void CTestFraction::equalTest (void)
    {
    // test successful, if true is returned
    CPPUNIT_ASSERT (*d == *e);
    CPPUNIT_ASSERT (CFraction (1) == CFraction (2, 2));
    CPPUNIT_ASSERT (CFraction (1) != CFraction (1, 2));
    // both must have equal valued
    CPPUNIT_ASSERT_EQUAL (*f, *g);
    CPPUNIT_ASSERT_EQUAL (*h, CFraction (0));
    CPPUNIT_ASSERT_EQUAL (*h, CFraction (0, 1));
    }
